package com.atguigu.mvc.util;

import javax.servlet.http.HttpServletRequest;

public class AtguiguUtil {

    /**
     * 判断请求类型的工具方法
     * @param request 要判断类型的请求对象
     * @return
     *      true：是 Ajax 请求
     *      false：不是 Ajax 请求
     */
    public static boolean judgeRequestType(HttpServletRequest request) {

        // 获取请求消息头
        String accept = request.getHeader("Accept");
        String xRequestedWith = request.getHeader("X-Requested-With");

        // 执行判断
        return ( (accept != null && accept.contains("application/json"))
                ||
                (xRequestedWith != null && xRequestedWith.contains("XMLHTTPRequest"))
                );
    }

}
